<?php

class Resources_View {

    function __construct() {
        
    }

    public function toString() {
        $view_wrapper = new Div_Container('id', 'resources-wrapper');
        $heading = new Div_Container('id', 'View_Heading');
        $body = new Div_Container('id', 'resources-body');

        $heading->addContent('<h1>Resources Page</h1></br></br>');

        $attrs = array('width' => '680', 'class' => 'gridtable');
        $table = new HTML_Table($attrs);
        $table->setAutoGrow(true);
        $table->setAutoFill('n/a');
        $table->setHeaderContents(0, 0, 'No.');
        $table->setHeaderContents(0, 1, 'Resource');
        $table->setHeaderContents(0, 2, 'Count');
        $table->setHeaderContents(0, 3, ' ');

        $resource_type_inventory = ResourceTypeDB::getAllResourceTypes();
        $data = array();
        if (!is_null($resource_type_inventory)) {
            $resource_type_inventory->rewind();
            while ($resource_type_inventory->valid()) {
                $current_item = $resource_type_inventory->current();
                $type_id = $current_item->getResource_type_id();
                $count = ResourceDB::getResourceCount($type_id);
                $list_btn = '<button onclick=window.location.href=' . '\'index.php?action=resources_list&resource_type_id=' . 
                        $type_id . '\'' . ' type="button" name="list" id="listResources">list</button>';
                $table_row_data = array($current_item->getResource_type_name(), (string) $count, $list_btn);
                $data[] = $table_row_data;
                $resource_type_inventory->next();
            }
            for ($nr = 0; $nr < count($data); $nr++) {
                $table->setHeaderContents($nr + 1, 0, (string) $nr + 1);
                for ($i = 0; $i < 3; $i++) {
                    if ('' != $data[$nr][$i]) {
                        $table->setCellContents($nr + 1, $i + 1, $data[$nr][$i]);
                    }
                }
            }
        } else {
            //no product types
        }


        $body->addContent($table->toHtml());
        $view_wrapper->addContent($heading->toString());
        $view_wrapper->addContent($body->toString());

        return $view_wrapper->toString();
    }

}

?>
